from user.models import User, Role


class AccountAlreadyExist(Exception):
    pass


def create_user(account, password, username=None, rid=1):
    username = account if username is None else username
    if User.objects.filter(id=account).exists():
        raise AccountAlreadyExist
    new_user = User(id=int(account), password=password, name=username, rid=Role.objects.get(id=rid))  # 1-root
    new_user.save()
    return new_user.id, new_user.name
